<?php


namespace app\admin\controller;

use app\model\web\Nav as NavModel;




/**
 * 导航管理
 */
class Nav extends BaseAdmin
{
    /**
     * 导航列表
     */
    public function lists()
    {
        $nav = new NavModel();
        if (request()->isAjax()) {
            $page = input('page', 1);
            $page_size = input('page_size', PAGE_LIST_ROWS);
            $search_text = input('search_text', '');

            $condition = [

            ];
            if (!empty($search_text)) {
                $condition[] = [ 'a.nav_title', 'like', '%' . $search_text . '%' ];
            }

            return $nav->getNavPageList($condition, $page, $page_size);
        } else {
            $this->forthMenu();

            return $this->fetch("nav/lists");
        }
    }

    /**
     * 添加导航
     */
    public function addNav()
    {
        $nav = new NavModel();
        if (request()->isAjax()) {
            $data = [
                'nav_title' => input('nav_title', ''),
                'nav_url' => input('nav_url', ''),
                'image' => input('image', ''),
                'slide_sort' => input('slide_sort', 0),

            ];
            return $nav->addNav($data);
        } else {
            return $this->fetch("nav/add_nav");
        }
    }

    /**
     * 编辑导航
     */
    public function editNav()
    {
        $nav_id = input('nav_id', '');
        $nav = new NavModel();
        if (request()->isAjax()) {
            $data = [
                'nav_title' => input('nav_title', ''),
                'nav_url' => input('nav_url', ''),
                'image' => input('image', ''),
                'slide_sort' => input('slide_sort', 0),
            ];
            return $nav->editNav($data, [ [ 'nav_id', '=', $nav_id ] ]);
        } else {
            $nav_info = $nav->getNavInfo($nav_id);
            $this->assign('nav_info', $nav_info[ 'data' ]);

            return $this->fetch("nav/edit_nav");
        }
    }

    /**
     * 修改导航字段
     */
    public function editNavField()
    {
        if (request()->isAjax()) {
            $nav = new NavModel();
            $type = input('type', '');
            $value = input('value', '');
            $nav_id = input('nav_id', '');
            $data = [
                $type => $value
            ];
            return $nav->editNav($data, [ [ 'nav_id', '=', $nav_id ] ]);
        }
    }

    /**
     * 删除导航
     */
    public function deleteNav()
    {
        if (request()->isAjax()) {
            $nav_ids = input('nav_ids', 0);
            $nav = new NavModel();
            return $nav->deleteNav([ [ 'nav_id', 'in', $nav_ids ] ]);
        }
    }

}